<script setup lang="ts">
import { ref, defineExpose } from "vue";
const formRef = ref(null);
const subDataList = ref([]);

function upRadioChange(type, index) {
  // 操作显隐
  if (type == 1) {
    // console.log("取消必填");
    if (subDataList.value[index].props.disabled) {
      subDataList.value[index].$required = false;
    }
    // 操作必填
  } else {
    // console.log("取消隐藏");
    if (subDataList.value[index].$required) {
      subDataList.value[index].props.disabled = false;
    }
  }
}

function setSubDataList(data) {
  console.log(data);
  subDataList.value = data;
}

defineExpose({ setSubDataList });
</script>
<template>
  <div class="formDiv">
    <el-form ref="formRef" label-width="140px">
      <el-row style="justify-content: center">
        <template v-for="(item, index) in subDataList" :key="index">
          <el-col :span="8">
            <el-form-item
              class="two_row"
              :label="item.title + '：'"
              prop="type"
              :required="item.$required"
            >
              <el-input disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item
              class="two_row"
              label="宽度(默认为一行的1/4)："
              label-width="200px"
              prop="type"
            >
              <el-input-number v-model="item.props.formRow" :min="1" :max="4" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div style="display: flex">
              <el-form-item :label="null" label-width="20px">
                <el-radio-group
                  v-model="item.props.disabled"
                  @change="upRadioChange('1', index)"
                >
                  <el-radio-button :value="false">显示</el-radio-button>
                  <el-radio-button :value="true">隐藏</el-radio-button>
                </el-radio-group>
              </el-form-item>
              <el-form-item :label="null" label-width="20px">
                <el-radio-group
                  v-model="item.$required"
                  @change="upRadioChange('2', index)"
                >
                  <el-radio-button :value="true">必填</el-radio-button>
                  <el-radio-button :value="false">非必填</el-radio-button>
                </el-radio-group>
              </el-form-item>
            </div>
          </el-col>
        </template>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
.formDiv {
  width: 80%;
  margin: 0 auto;
  margin-top: 20px;
}
</style>
